package com.hcbd.tdwt.base.other;

import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;

public class CodeModeInputFilter implements InputFilter {

    private String TAG = "CodeModeInputFilter";
    // a as asd asdf
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//        // 允许的字符范围（A-F、a-f、0-9）
        StringBuilder builder = new StringBuilder();
        String allowedChars = "ABCDEFabcdef0123456789";
        // 检查输入的字符是否在允许范围内

        Log.e(TAG, "输入: "+source+" start:"+start+" end:"+end+" dest:"+dest+" dstart:"+dstart+" dend:"+dend);
        for (int i = start; i < end; i++) {
            String value = String.valueOf(source.charAt(i));
            if (!allowedChars.contains(value)) {
                if(onFilterWork!=null){onFilterWork.onIllegalInput();}
            }else{
                builder.append(value);
            }
        }

//        if (builder.length() == end - start) {
//            return null;
//        } else {
//            return builder.toString().toUpperCase();
//        }
        return builder.toString().toUpperCase();
    }

    public interface OnFilterWork{
        void onIllegalInput();
    }
    private OnFilterWork onFilterWork;
    public void setOnFilterWork(OnFilterWork onFilterWork){this.onFilterWork=onFilterWork;}
}
